package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportDefinition;
import com.simon.annual.repositoy.IReportDefinitionRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
import com.simon.annual.service.IExplanation;

/**
 * 处理释义项目
 * */
@Service
public class FindExplanationImpl implements IExplanation{

	@Resource
	private IReportDefinitionRepositoy reportDefinition;	
	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private Environment env;
	
	@Override
	public void analysis(Document doc, int companyPtrId) {
		//释义项目
		Elements  definitions = doc.getElementsMatchingOwnText(env.getProperty("pdf.definitions"));//释义 项目
		
		for (Element element : definitions) {
			if(element.parent().tagName().equals("taggedpdf-doc")&&element.nextElementSibling().tagName().equals("table")){
				element = cmpanyPublicInfo.findNextTag("table", element);
				Elements tr = element.select("table tr");
				List<ReportDefinition>  reportDefinitionList = new ArrayList<ReportDefinition>();
				for (int i = 1; i < tr.size(); i++) {
					ReportDefinition tReportDefinition = new ReportDefinition();
					Elements eTd = tr.get(i).children();
					if(eTd.size()>1){
						tReportDefinition.setAbbr(eTd.first().text());
						tReportDefinition.setMeaning(eTd.last().text());
						tReportDefinition.setProjectScopeId(companyPtrId);
						reportDefinitionList.add(tReportDefinition);
					}
				}
				reportDefinition.save(reportDefinitionList);
				break;
			}
		}
	}
}
